登录 白背景

1816. 截断句子

https://leetcode-cn.com/problems/truncate-sentence/

  • 提交时间:2021-12-05 16:27:59
  • 执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
  • 内存消耗:2.1 MB, 在所有 Go 提交中击败了100.00%的用户
  • 通过测试用例:72 / 72
func truncateSentence(s string, k int) string {
    for index, word := range s {
        if string(word) != " " {
            continue
        }
        k--
        if k == 0 {
            return s[:index]
        }
    }
    return s
}